package com.qh.yyxt.controller;

import com.qh.yyxt.config.UserRealm;
import com.qh.yyxt.pojo.entity.meeting.User;
import com.qh.yyxt.service.BorrowService;
import com.qh.yyxt.service.UserService;
//import com.sun.deploy.net.HttpResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController
{
    @Autowired
    private UserService userService;
    @Autowired
    private BorrowService borrowService;
    @RequestMapping(value = {"/login"})
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/index")
    public String toIndex(){
        return "index";
    }
    @RequestMapping("/register")
    public String toRegister(){
        return "register";
    }
    @RequestMapping("/thingsShow")
    public String aa(Model model,HttpSession session){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        session.setAttribute("user",user);
        return "thingsShow";
    }
//    @RequestMapping("/user/register")
//    public String register(String username,String password1,String password2, String tel, HttpSession session)
//    {
//
//        if(username==null||password1==null||tel==null)
//        {
//            return "redirect:/register";
//        }
//        String pwd="";
//        if(password1.equals(password2))
//        {
//            pwd=password1;
//        }else{
//            session.setAttribute("msg","密码不一致！");
//            return "redirect:/register";
//        }
//        //生成uuid
//        String salt = UUID.randomUUID().toString();
//        /*
//        MD5加密:
//        *使用SimpleHash类对原始密码进行加密。
//        *第一个参数代表使用MD5方式加密 第二个参数为原始密码
//        *第三个参数为盐值，即用户名第四个参数为加密次数
//            *最后用toHex()方法将加密后的密码转成String*/
//        String newPwd=new SimpleHash("MD5",pwd,salt,1024).toHex();
//        User user=userService.findByUsername(username);
//        if( userService.register(username,newPwd,salt,tel,"0")==1)
//        {
//            session.setAttribute("msg","注册成功请登录！");
//            return "redirect:/login";
//        }else{
//            session.setAttribute("msg","用户名已存在！");
//            return "redirect:/register";
//        }
//    }

    @RequestMapping("/")
    public String index(){
        return "login";
    }


    @RequestMapping("/user/login")
    public String login(String username,String password,HttpSession session)
    {
        //  System.out.println("login");
        if(username==null||password==null)
        {
            return "redirect:/login";
        }
        if(username.equals("")||password.equals(""))
        {
            return "redirect:/login";
        }
        //获取当前用户
        Subject subject= SecurityUtils.getSubject();
        //封装用户登录数据
        try{
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            subject.login(token);//执行登录，如果没有异常 就可以
            session.setAttribute("username",username);
            User u = userService.findByUsername(username);
            session.setAttribute("name",u.getName());
            session.setAttribute("role",u.getRole());
            return "redirect:/index";
        }catch (UnknownAccountException e)//用户名不存在
        {
            session.setAttribute("msg","用户名错误");
            //model.addAttribute("msg","用户名错误");
            return "redirect:/login";
        }catch (IncorrectCredentialsException e)//密码错误
        {
            session.setAttribute("msg","密码错误！");
            // model.addAttribute("msg","密码错误");
            return "redirect:/login";
        }
    }
    @RequestMapping("/user/changePwd")
    @ResponseBody
    //旧密码 新密码 确认密码
    public String changePwd(String password,String newPwd,String cfgPwd)
    {
        return  userService.updatePassword(password,newPwd,cfgPwd);
    }
    @RequestMapping("/user/changeInfo")
    @ResponseBody
    public String changeInfo(String tel1,String tel2,String tel3)
    {
        return userService.updateInfo(tel1,tel2,tel3);
    }
    @RequestMapping("/user/judgeOldPwd")
    @ResponseBody
    public String judgeOldPwd(String oldPwd)
    {
        return userService.judgeOldPwd(oldPwd);
    }
    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login";
    }
    @RequestMapping("/user/getInfo")
    @ResponseBody
    public Map<String,Object> getInfo(){
        User user=(User)SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map=new HashMap<>();
        map.put("name",user.getName());
        map.put("tel1",user.getTel1());
        map.put("tel2",user.getTel2());
        map.put("tel3",user.getTel3());
        return map;
    }
//    @RequestMapping("/user/test")
//    @ResponseBody
//    public void test( HttpSession session) throws Exception {
//        File file=new File("E:\\杂物\\Tencent Files\\1.xls");
//        XSSFWorkbook book = new XSSFWorkbook(new FileInputStream(file));
//
//        XSSFSheet sheet = book.getSheetAt(0);
//
//        for(int i=1; i<sheet.getLastRowNum()+1; i++) {
//            XSSFRow row = sheet.getRow(i);
//            String id = row.getCell(0).getStringCellValue(); //名称
//            String name = row.getCell(1).getStringCellValue(); //url
//            String username = row.getCell(2).getStringCellValue();
//            String password = row.getCell(3).getStringCellValue();
//            User user=new User();
//            user.setName(name);
//            user.setPassword(password);
//            user.setRole("0");
//            user.setUsername(username);
//            register(username,password,name,session);
//        }
//
//    }
}

